package com.mstore.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.mstore.interceptor.Authority;
import com.mstore.pojo.Account;
import com.mstore.pojo.BFuncPrivilege;
import com.mstore.pojo.BFunction;
import com.mstore.service.AccountService;
import com.mstore.service.BFuncPrivilegeService;
import com.mstore.service.BFunctionService;

public class FunctionAction extends BaseAction {

	private static final long serialVersionUID = 2237558049193822864L;

	private BFunctionService functionService;

	private BFuncPrivilegeService funcPrivilegeService;

	private AccountService accountService;

	private List<BFunction> functions;

	private BFunction function;

	private String functionName;

	private Long functionId;

	private String result;

	public String watchAllFunction() {
		functions = functionService.getAllFunction();
		return "watchAllFunction";
	}

	/**
	 * 添加功能
	 */
	public String addFunction() {
		if (function == null) {
			result = "系统异常！";
			return "error";
		}

		String name = function.getName();
		if (StringUtils.isBlank(name)) {
			result = "请填写功能名称！";
			return "error";
		}

		functionService.addFunction(function);

		List<Account> accounts = accountService.tmpGetAll();
		for (Account account : accounts) {
			Long eaId = account.getId();
			BFuncPrivilege funcPrivilege = new BFuncPrivilege();
			funcPrivilege.setEaId(eaId);
			funcPrivilege.setFunctionId(function.getId());
			switch (function.getLevel()) {
				case 0: // 超级管理员权限功能
					if (eaId == 2) {
						funcPrivilege.setOnOff("1");
					}
					else {
						funcPrivilege.setOnOff("0");
					}
					break;
				case 1: // 商户权限功能
					if (eaId == 2) {
						funcPrivilege.setOnOff("0");
					}
					else {
						funcPrivilege.setOnOff("1");
					}
					break;
				case 2: // 通用
					funcPrivilege.setOnOff("1");
					break;
			}

			funcPrivilegeService.addFuncPrivilege(funcPrivilege);
		}

		return SUCCESS;
	}

	/**
	 * 添加时验证功能名称是否重复
	 */
	public String verifyFunctionName() {
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isBlank(functionName)) {
			map.put("temp", "error");
			result = mapTojson(map);
			return "verifyFunctionName";
		}

		boolean verify = functionService.verifyFunctionName(functionName);
		if (verify) {
			map.put("temp", "ok");
		}
		else {
			map.put("temp", "wrong");
		}
		result = mapTojson(map);

		return "verifyFunctionName";
	}

	/**
	 * 修改时验证功能名称是否重复
	 */
	public String verifyFunctionNameExceptSelf() {
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isBlank(functionName) || functionId == null) {
			map.put("temp", "error");
			result = mapTojson(map);
			return "verifyFunctionName";
		}

		boolean verify = functionService.verifyFunctionNameExceptSelf(
				functionName, functionId);
		if (verify) {
			map.put("temp", "ok");
		}
		else {
			map.put("temp", "wrong");
		}
		result = mapTojson(map);

		return "verifyFunctionName";
	}

	/**
	 * 删除功能
	 */
	public String delFunction() {
		if (function == null) {
			result = "系统异常！";
			return "error";
		}

		if (function.getId() == null) {
			result = "系统异常！";
			return "error";
		}
		functionService.delFunction(function);
		return SUCCESS;
	}

	/**
	 * 跳转到功能修改页面
	 */
	public String goToModifyFunction() {
		if (function == null) {
			result = "系统异常！";
			return "error";
		}
		if (function.getId() == null) {
			result = "系统异常！";
			return "error";
		}

		function = functionService.getFunctionById(function.getId());

		if (function == null) {
			result = "该功能不存在！";
			return "error";
		}

		return "goToModifyFunction";
	}

	/**
	 * 修改功能
	 */
	public String modifyFunction() {
		if (function == null) {
			result = "系统异常！";
			return "error";
		}

		functionService.modifyFunction(function);

		List<BFuncPrivilege> funcPrivileges = funcPrivilegeService
				.getFuncPrivilegeByFunctionId(function.getId());

		for (BFuncPrivilege funcPrivilege : funcPrivileges) {
			switch (function.getLevel()) {
				case 0: // 超级管理员权限功能
					if (funcPrivilege.getEaId() == 2) {
						funcPrivilege.setOnOff("1");
					}
					else {
						funcPrivilege.setOnOff("0");
					}
					break;
				case 1: // 商户权限功能
					if (funcPrivilege.getEaId() == 2) {
						funcPrivilege.setOnOff("0");
					}
					else {
						funcPrivilege.setOnOff("1");
					}
					break;
				case 2: // 通用
					funcPrivilege.setOnOff("1");
					break;
			}
			funcPrivilegeService.modifyFuncPrivilegeOnOff(funcPrivilege);
		}

		return SUCCESS;
	}

	/**
	 * 临时脚本
	 */
	@Authority(actionName = "maccount", privilege = "pass")
	public String tempMethod() {
		List<Account> accounts = accountService.tmpGetAll();
		List<BFunction> functions = functionService.getAllFunction();
		for (BFunction function : functions) {
			for (Account account : accounts) {
				BFuncPrivilege funcPrivilege = funcPrivilegeService
						.getFuncPrivilegeByFuncIdAndEaId(account.getId(),
								function.getId());
				if (funcPrivilege == null) {
					funcPrivilege = new BFuncPrivilege();
					funcPrivilege.setEaId(account.getId());
					funcPrivilege.setFunctionId(function.getId());
					switch (function.getLevel()) {
						case 0:
							if (account.getId() == 2) {
								funcPrivilege.setOnOff("1");
							}else {
								funcPrivilege.setOnOff("0");
							}
							break;
						case 1:
							if (account.getId() == 2) {
								funcPrivilege.setOnOff("0");
							}else {
								funcPrivilege.setOnOff("1");
							}
							break;
						case 2:
							funcPrivilege.setOnOff("1");
							break;
					}
					funcPrivilegeService.addFuncPrivilege(funcPrivilege);
				}
			}
		}
		return NONE;
	}

	public BFunctionService getFunctionService() {
		return functionService;
	}

	public void setFunctionService(BFunctionService functionService) {
		this.functionService = functionService;
	}

	public List<BFunction> getFunctions() {
		return functions;
	}

	public void setFunctions(List<BFunction> functions) {
		this.functions = functions;
	}

	public BFunction getFunction() {
		return function;
	}

	public void setFunction(BFunction function) {
		this.function = function;
	}

	public String getFunctionName() {
		return functionName;
	}

	public void setFunctionName(String functionName) {
		this.functionName = functionName;
	}

	public Long getFunctionId() {
		return functionId;
	}

	public void setFunctionId(Long functionId) {
		this.functionId = functionId;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public BFuncPrivilegeService getFuncPrivilegeService() {
		return funcPrivilegeService;
	}

	public void setFuncPrivilegeService(
			BFuncPrivilegeService funcPrivilegeService) {
		this.funcPrivilegeService = funcPrivilegeService;
	}

	public AccountService getAccountService() {
		return accountService;
	}

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

}
